package com.valuegroup.smarttodo.data.service;

import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.SumContent;
import com.valuegroup.smarttodo.data.model.SumTopic;
import com.valuegroup.smarttodo.data.model.Summarize;
import com.valuegroup.smarttodo.data.model.SummarizeDao;
import com.valuegroup.smarttodo.plugins.pickdialog.DateUtils;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Date;
import java.util.List;

public class SummarizeService extends BaseService {

    public List<Summarize> findSummarize(int qType, int qYear, int qMonth) {
        System.out.println("qType:" + qType + " qYear:" + qYear + " qMonth:" + qMonth);

        /*if (getSummarizeDao().loadAll().size() > 0) {
            getSummarizeDao().deleteAll();
            getSumContentDao().deleteAll();
            getSumTopicDao().deleteAll();
        }

        if (getSummarizeDao().loadAll().size() == 0) {
            System.out.println("生成新数据");
            SumTopic topic;
            if (getSumTopicDao().loadAll().size() == 0) {
                topic = ServiceFactory.getInstance().getSumTopicService().getDefaultSumTopic();
                topic.setTitle("主题测试");
                getSumTopicDao().save(topic);
            } else {
                topic = getSumTopicDao().loadAll().get(0);
            }

            Date day = new Date();
            int year = DateUtils.getYear(day);

            Summarize sumDay = getDefaultSummarizeForDay(day);
            getSummarizeDao().save(sumDay);
            Summarize sumWeek = getDefaultSummarizeForWeek(year, DateUtils.getWeekOfYear(day));
            getSummarizeDao().save(sumWeek);
            Summarize sumMonth = getDefaultSummarizeForMonth(year, DateUtils.getMoth(day));
            getSummarizeDao().save(sumMonth);
            Summarize sumQuarter = getDefaultSummarizeForQuarter(year, DateUtils.getQuarter(day));
            getSummarizeDao().save(sumQuarter);
            Summarize sumYear = getDefaultSummarizeForYear(year);
            getSummarizeDao().save(sumYear);

            SumContent scDay = ServiceFactory.getInstance().getSumContentService().getDefaultSumContent(sumDay.getId(), topic.getId());
            scDay.setContent("xxxxx");
            getSumContentDao().save(scDay);
            SumContent scWeek = ServiceFactory.getInstance().getSumContentService().getDefaultSumContent(sumWeek.getId(), topic.getId());
            scWeek.setContent("xxxxx");
            getSumContentDao().save(scWeek);
            SumContent scMonth = ServiceFactory.getInstance().getSumContentService().getDefaultSumContent(sumMonth.getId(), topic.getId());
            scMonth.setContent("xxxxx");
            getSumContentDao().save(scMonth);
            SumContent scQuarter = ServiceFactory.getInstance().getSumContentService().getDefaultSumContent(sumQuarter.getId(), topic.getId());
            scQuarter.setContent("xxxxx");
            getSumContentDao().save(scQuarter);
            SumContent scYear = ServiceFactory.getInstance().getSumContentService().getDefaultSumContent(sumYear.getId(), topic.getId());
            scYear.setContent("xxxxx");
            getSumContentDao().save(scYear);

        }*/

        QueryBuilder<Summarize> q = getSummarizeDao().queryBuilder();
        q.where(SummarizeDao.Properties.SumType.eq(qType));
        q.where(SummarizeDao.Properties.SumYear.eq(qYear));
        if (qType == AppStatus.SumType.DAY) {
            q.where(SummarizeDao.Properties.SumMonth.eq(qMonth));
            q.orderAsc(SummarizeDao.Properties.SumDay);
        } else if (qType == AppStatus.SumType.MONTH) {
            q.orderAsc(SummarizeDao.Properties.SumMonth);
        } else if (qType == AppStatus.SumType.QUARTER) {
            q.orderAsc(SummarizeDao.Properties.SumQuarter);
        } else if (qType == AppStatus.SumType.YEAR) {
            q.orderAsc(SummarizeDao.Properties.SumYear);
        } else if (qType == AppStatus.SumType.WEEK) {
            q.orderAsc(SummarizeDao.Properties.SumWeek);
        }

        q.where(SummarizeDao.Properties.Status.eq(AppStatus.Status.NORMAL));

        return q.list();
    }

    public Summarize checkExistAndSaveSummarize(Summarize t) {
        QueryBuilder<Summarize> q = getSummarizeDao().queryBuilder();
        q.where(SummarizeDao.Properties.SumType.eq(t.getSumType()));
        q.where(SummarizeDao.Properties.SumDay.eq(t.getSumDay()));
        q.where(SummarizeDao.Properties.SumWeek.eq(t.getSumWeek()));
        q.where(SummarizeDao.Properties.SumMonth.eq(t.getSumMonth()));
        q.where(SummarizeDao.Properties.SumQuarter.eq(t.getSumQuarter()));
        q.where(SummarizeDao.Properties.SumYear.eq(t.getSumYear()));
        q.where(SummarizeDao.Properties.Status.eq(AppStatus.Status.NORMAL));
        List<Summarize> search = q.list();

        if (search.size() == 0) {
            getSummarizeDao().save(t);
            return t;
        } else {
            return search.get(0);
        }
    }


    public Summarize getDefaultSummarizeForDay(Date day) {
        Summarize t = getDefaultSummarize();
        t.setSumType(AppStatus.SumType.DAY);
        t.setSumDay(DateUtils.getDay(day));
        t.setSumYear(DateUtils.getYear(day));
        t.setSumMonth(DateUtils.getMoth(day));
        t.setTitle(DateUtils.date2str(day, DateUtils.F31));
        return t;
    }

    public Summarize getDefaultSummarizeForWeek(int sumYear, int sumWeek) {
        Summarize t = getDefaultSummarize();
        t.setSumType(AppStatus.SumType.WEEK);
        t.setSumYear(sumYear);
        t.setSumWeek(sumWeek);
        t.setTitle(sumYear + "年" + sumWeek + "周");
        return t;
    }

    public Summarize getDefaultSummarizeForQuarter(int sumYear, int sumQuarter) {
        Summarize t = getDefaultSummarize();
        t.setSumType(AppStatus.SumType.QUARTER);
        t.setSumYear(sumYear);
        t.setSumQuarter(sumQuarter);
        t.setTitle(sumYear + "年" + sumQuarter + "季度");
        return t;
    }

    public Summarize getDefaultSummarizeForMonth(int sumYear, int sumMonth) {
        Summarize t = getDefaultSummarize();
        t.setSumType(AppStatus.SumType.MONTH);
        t.setSumYear(sumYear);
        t.setSumMonth(sumMonth);
        t.setTitle(sumYear + "年" + sumMonth + "月");
        return t;
    }

    public Summarize getDefaultSummarizeForYear(int sumYear) {
        Summarize t = getDefaultSummarize();
        t.setSumType(AppStatus.SumType.YEAR);
        t.setSumYear(sumYear);
        t.setTitle(sumYear + "年");
        return t;
    }


    private Summarize getDefaultSummarize() {
        Summarize t = new Summarize();
        t.setSumDay(0);
        t.setSumMonth(0);
        t.setSumQuarter(0);
        t.setSumType(0);
        t.setSumWeek(0);
        t.setSumYear(0);
        t.setTitle("");
        t.setFinishDate(null);
        t.setIsFinish(false);
        t.setCreatedDt(new Date());
        t.setUpdatedDt(t.getCreatedDt());
        t.setStatus(AppStatus.Status.NORMAL);
        return t;
    }

    public void delSum(Long id) {
        Summarize sum = getSummarizeDao().load(id);
        if (sum == null) {
            return;
        }

        ServiceFactory.getInstance().getSumContentService().clearTopic(id);
        getSummarizeDao().delete(sum);
    }
}
